package com.wswy.wzcx.ui.dmv;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.TextView;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.blankj.utilcode.util.SizeUtils;
import com.che.libcommon.utils.MapUtils;
import com.che.libcommon.utils.view.FABUtils;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.dmv.DepartmentInfo;
import com.wswy.wzcx.ui.activity.SampleBaseActivity;

public class CarDepartmentDetailActivity extends SampleBaseActivity {

  private static final String TAG = "CarDepartmentDetailActi";

  public static void start(Context context,DepartmentInfo departmentInfo){
    Intent intent = new Intent(context,CarDepartmentDetailActivity.class);
    intent.putExtra("args.data",departmentInfo);
    context.startActivity(intent);
  }

  private TextView tvName;
  private TextView tvInfo;
  private  TextView tvWorkTime;
  private TextView tvServiceScope;


  private  MapView mapView;

  private FloatingActionButton fabNav;


  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_car_dvm_detail);

    tvName = findViewById(R.id.tv_name);
    tvInfo = findViewById(R.id.tv_info);
    tvWorkTime = findViewById(R.id.tv_time_title);
    tvServiceScope = findViewById(R.id.tv_service_scope);
    mapView = findViewById(R.id.mapView);
    fabNav = findViewById(R.id.fab_navigation);
    fabNav.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onFabClick(v);
      }
    });


    setAppTitle("车管所详情");

    mapView.onCreate(savedInstanceState);


    Intent intent = getIntent();
    if(intent != null && intent.hasExtra("args.data")){
      DepartmentInfo departmentInfo = intent.getParcelableExtra("args.data");
      if(departmentInfo != null){


        tvName.setText(departmentInfo.name);
        tvInfo.setText(getString(R.string.dmv_info,departmentInfo.distancKM,departmentInfo.address,departmentInfo.tel));
        tvWorkTime.setText(getString(R.string.dmv_work_time,departmentInfo.workTime));

        tvServiceScope.setText(getString(R.string.dmv_work_scope,departmentInfo.scope));

        AMap aMap = mapView.getMap();
        aMap.getUiSettings().setZoomControlsEnabled(false);

        LatLng latLng = new LatLng(departmentInfo.lat, departmentInfo.lon);
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
            latLng,16));

        BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
            .fromResource(R.drawable.icon_marka);

        MarkerOptions options = new MarkerOptions();
        options.icon(mCurrentMarker);
        options.anchor(0.5f, 0.5f);
        options.position(latLng);

        aMap.addMarker(options);

      }

    }


    fabNav.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {

        fabNav.animate().yBy(fabNav.getHeight()/2).setDuration(0).start();

        fabNav.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      }
    });


    fabNav.setImageBitmap(FABUtils.createBitmap(this,R.drawable.map_navigation,
        SizeUtils.dp2px(17),
        SizeUtils.dp2px(23),
        getString(R.string.map_navigation),14, Color.WHITE
    ));

  }

  private void onFabClick(View view){

    DepartmentInfo departmentInfo = getIntent().getParcelableExtra("args.data");
    if(departmentInfo != null){

      Intent intent = MapUtils.queryNavigation(departmentInfo.lat, departmentInfo.lon);
      try{
        startActivity(intent);
      }catch (Exception e){
        e.printStackTrace();
      }

    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }

  @Override
  protected void onResume() {
    super.onResume();
    mapView.onResume();
  }

  @Override
  protected void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if(mapView != null){
      mapView.onDestroy();
    }
  }
}
